package com.sketch.controller;

import com.sketch.commom.Result;
import com.sketch.commom.config.properties.WechatProperties;
import com.sketch.service.RechargeRecordService;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.exception.ValidationException;
import com.wechat.pay.java.core.notification.NotificationConfig;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.core.notification.RequestParam;
import com.wechat.pay.java.service.payments.h5.H5Service;
import com.wechat.pay.java.service.payments.h5.model.*;
import com.wechat.pay.java.service.payments.model.Transaction;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@Api(tags = "微信支付 API")
@RequestMapping(value = "/api/wechat")
@Slf4j
public class WechatPayController {


    @Autowired
    private RechargeRecordService rechargeRecordService;

    @Autowired
    private WechatProperties wechatProperties;

    /**
     * 订单支付
     */
    @ApiOperation("微信支付")
    @PostMapping("/orderPay")
    public Result<String> orderPay(HttpServletRequest httpServletRequest) {
//        Config config =
//                new RSAAutoCertificateConfig.Builder()
//                        .merchantId(merchantId)
//                        .privateKeyFromPath(this.getClass().getClassLoader().getResource(privateKeyPath).getPath())
//                        .merchantSerialNumber(merchantSerialNumber)
//                        .apiV3Key(apiV3Key)
//                        .build();
//        // 构建service
//        H5Service builder = new H5Service.Builder()
//                .config(config).build();
//        PrepayRequest request =new PrepayRequest();
//
//        Amount amount = new Amount();
//        amount.setTotal(100);
//        request.setAmount(amount);
//        request.setAppid(appid);
//        SceneInfo sceneInfo=new SceneInfo();
//        sceneInfo.setPayerClientIp("192.142.121.12");
//        //sceneInfo.setDeviceId();
//        //sceneInfo.setStoreInfo();
//        H5Info h5Info = new H5Info();
//        h5Info.setType("h5");
//        h5Info.setAppName("社区");
//        sceneInfo.setH5Info(h5Info);
//        request.setSceneInfo(sceneInfo);
//        request.setMchid(merchantId);
//        request.setDescription("测试商品标题");
//        request.setNotifyUrl("https://notify_url/callBack?orderId=12455");
//        request.setOutTradeNo("out_trade_no_001");
//        PrepayResponse prepay = null;
//        try {
//            prepay = builder.prepay(request);
//        } catch (Exception e) {
//            log.error("支付错误",e);
//            prepay = new PrepayResponse();
//            prepay.setH5Url("https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?prepay_id=wx2016121516420242444321ca0631331346&package=1405458241");
//            //throw new RuntimeException(e);
//        }
//        return  Result.success(prepay.getH5Url());
        return null;
    }

}
